<?php
require_once('../model/m_schedules.php');
require_once('../model/m_users.php');

class staffing_schedules{

	public $usersData = array();
	public $data = array();
	public $weedaydata = array();
	public $shiftdata = array();
	public $qshiftdata = array();
	public $currentUser= "";
	//public $current_role;
	
	public function actionView(){
		$this->usersData = getUsuarios(); //Get list of users	
		include_once("../views/v_session.php");
		include_once("../views/v_header_principal_staffing_schedules_ajax.html");
		include_once("../views/v_staffing_schedules.html");
		include_once("../views/v_footer_principal.html");
	}	
	
	public function actionViewUser($username){
		$this->currentUser = $username;
		$this->data = getSchedule($username); //Get schedule for specific user
		include_once("../views/_v_staffing_schedules_user.html");
	}

	public function actionAdd($username){	
		$this->currentUser = $username;	
		$this->data = getSchedule($username);
		$this->weekdaydata = loadWeekdays();
		$this->shiftdata = loadShifts();
		$this->qshiftdata = loadQShifts();
		include_once("../views/_v_staffing_schedules_user_add.html");
	}
	
	public function actionAddScheduleUser($user,$weekday,$shift,$qshift){	
		$this->currentUser = $username;	
		if(addScheduleUser($user,$weekday,$shift,$qshift)){
			header("location:c_staffing_schedules.php?viewUser=".$user);
			echo "Schedule added Successfully!";
			exit;				
		} else {		
			echo "<h1> Error creating Schedule for User. Make sure the user doesn't already have a schedule for that day or something...</h1>";
			exit;
		}	
	}	

	public function actionEditSchedule($username,$scheduleId){	
		$this->currentUser = $username;	
		$this->data = getUserSchedule($username,$scheduleId);
		$this->shiftdata = loadShifts();
		$this->qshiftdata = loadQShifts();		
		///*** FALTA MANEJAR Schedule INEXISTENTE
		include_once("../views/_v_staffing_schedules_user_edit.html");
	}	

	public function actionEditSaveUserSchedule($username,$scheduleId,$weekday,$shift,$qshift){
		$this->currentUser = $username;	
		
		if(saveScheduleUser($username,$scheduleId,$weekday,$shift,$qshift)){
			header("location:c_staffing_schedules.php?viewUser=".$username);
			exit;
		}
		else {
			echo "<h1>THERE WAS A PROBLEM WHEN TRYING TO SAVE THE CHANGES!!!</h1>";
		}	
	
	}

	public function actionDeleteSchedule($username,$scheduleId){	
		$this->currentUser = $username;	
		if(deleteUserSchedule($username,$scheduleId))
			header("location:c_staffing_schedules.php?viewUser=".$username);
		///*** FALTA MANEJAR Schedule INEXISTENTE
	}		
}

$instancia= new staffing_schedules;

if(isset($_GET['view'])){
	if($_GET['view']=="list")	
		$instancia->actionView();
}

if(isset($_GET['viewUser'])){
	$instancia->actionViewUser($_GET['viewUser']);
}

if(isset($_GET['add'])){
	$instancia->actionAdd($_GET['user']);
}

if(isset($_GET['addsave'])){
	if($_GET['addsave']=="yes")	
		$instancia->actionAddScheduleUser($_GET['user'], $_GET['weekday'], $_GET['shift'], $_GET['qshift']);
}

if(isset($_GET['edit'])){
		$instancia->actionEditSchedule($_GET['edit'],$_GET['schedule']);
}

if(isset($_GET['editsave'])){
	if($_GET['editsave']=="true")
		$instancia->actionEditSaveUserSchedule($_GET['user'],$_GET['schedule'],$_GET['weekday'],$_GET['shift'],$_GET['qshift']);
}

if(isset($_GET['delete'])){
		$instancia->actionDeleteSchedule($_GET['delete'],$_GET['schedule']);
}


?>